package cn.itsource.pet.service.impl;


import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.PetDetail;
import cn.itsource.pet.domain.UpDownDto;
import cn.itsource.pet.mapper.PetDetailMapper;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.query.PetQuery;
import cn.itsource.pet.service.IPetService;
import cn.itsource.shop.mapper.ShopMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 万能键 alt + enter
 */
@Service //交给spring管理
public class PetServiceImpl extends BaseServiceImpl<Pet, PetQuery> implements IPetService {

    /**
     * 注入mapper层对象
     */
    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper detailMapper;

    @Override
    public PageList<Pet> findPage(PetQuery q) {

        //分页查询
        //1.分页工具包
        PageHelper.startPage(q.getPage(), q.getSize());

        //2.调用方法
        List<Pet> ts = petMapper.findPage();

        //3.封装一个pageInfo对象
        PageInfo<Pet> pageInfo = new PageInfo<>(ts);

        //4.封装自定义的分页对象
        PageList<Pet> pageList = new PageList<>(pageInfo.getTotal(), pageInfo.getList());

        return pageList;
    }

    @Override
    @Transactional //就近原则
    public void saveOrUpdate(Pet pet) {

        //新增
        if (pet.getId() == null){


            //只有设置了值的字段才会被新增进去
            petMapper.insertSelective(pet);

            PetDetail petDetail = new PetDetail();
            petDetail.setPetId(pet.getId());//null
            petDetail.setAdoptNotice(pet.getAdoptNotice());
            petDetail.setIntro(pet.getIntro());

            //新增宠物详情信息
            detailMapper.insertSelective(petDetail);


        }else {
            //根据主键修改,只有设置了值的字段才会被新增进去
            petMapper.updateByPrimaryKey(pet);


            PetDetail petDetail = new PetDetail();
            petDetail.setPetId(pet.getId());//null
            petDetail.setAdoptNotice(pet.getAdoptNotice());
            petDetail.setIntro(pet.getIntro());

            detailMapper.updateByPetId(petDetail);

        }
    }


    @Override
    public Pet findOne(Long id) {
        Pet pet = petMapper.findOne(id);
        return pet;
    }


    @Override
    public AjaxResult changeState(UpDownDto upDownDto) {
        Long[] ids = upDownDto.getIds();

        for (Long id : ids) {
            Pet pet = new Pet();
            pet.setId(id);
            pet.setState(upDownDto.getState());

            petMapper.updateByPrimaryKeySelective(pet);
        }

        return AjaxResult.createSuccess();
    }

    @Override
    public PageList<Pet> queryHomePage(PetQuery petQuery) {
        //1.使用分页插件
        PageHelper.startPage(petQuery.getPage(), petQuery.getSize());

        //2.调用语句  where  state = 1的结果
        List<Pet> pets =  petMapper.queryHomePage(petQuery);

        //3.封装pageInfo对象
        PageInfo<Pet> petPageInfo = new PageInfo<>(pets);

        //4.返回pageList对象
        return new PageList<>(petPageInfo.getTotal(), petPageInfo.getList());


    }

    @Override
    public AjaxResult detailById(Long id) {

        Pet pet = petMapper.detailById(id);

        return AjaxResult.createSuccess(pet);
    }
}
